<template>
  <div>
    <el-card>
      <h3>添加商品</h3>
      <el-form ref="form" :model="addProduct" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="addProduct.name"></el-input>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input v-model="addProduct.subtitle"></el-input>
        </el-form-item>
        <el-form-item label="所属分类">
          <el-select
            @change="choseO"
            v-model="selectO"
            placeholder="请选择一级品类"
          >
            <el-option
              v-for="item in selectOne"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
          <el-select
            v-if="selectO !== ''"
            v-model="addProduct.categoryId"
            placeholder="请选择二级品类"
          >
            <el-option
              v-for="item in selectTwo"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input type="number" v-model="addProduct.price">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="商品库存">
          <el-input type="number" v-model="addProduct.stock">
            <template slot="append">件</template>
          </el-input>
        </el-form-item>
        <el-form-item label="上传图片">
          <el-upload
            action="/api/manage/product/upload.do"
            :on-success="handleSuccess"
            list-type="picture"
            :auto-upload="true"
            name="upload_file"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品详情">
          <quill-editor
            v-model="addProduct.detail"
            ref="myQuillEditor"
          ></quill-editor>
        </el-form-item>
      </el-form>
      <el-button @click="add">提交</el-button>
    </el-card>
  </div>
</template>

<script>
import { productType } from "../../../util/http";
import { productAdd } from "../../../util/http";
export default {
  // 组件名称
  name: "demo", // 组件参数 接收来自父组件的数据
  props: [],
  components: {},
  data() {
    return {
      selectOne: [],
      selectO: "",
      selectTwo: [],
      selectT: "",
      imageUrl: "",
      addProduct: {
        categoryId: "",
        name: "",
        subtitle: "",
        subImages: "",
        detail: "",
        price: "",
        stock: "",
        status: "1",
      },
    };
  },
  computed: {},
  watch: {},
  methods: {
    // 一二级接口
    async getSelectOne() {
      const { data: res } = await productType(
        `/api/manage/category/get_category.do?categoryId=0`
      );
      if (res.status !== 0) return this.$message.error("获取数据失败");
      this.$message.success("获取数据成功");
      this.selectOne = res.data;
      //   console.log(res.data);
    },
    async choseO() {
      const { data: res } = await productType(
        `/api/manage/category/get_category.do?categoryId=${this.selectO}`
      );
      this.selectTwo = res.data;
    },
    handleRemove(file, fileList) {
      console.log(file);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleSuccess(response) {
      this.addProduct.subImages = response.data.uri;
    },
    async add() {
      const { data: res } = await productAdd(
        `/api/manage/product/save.do?categoryId=${this.addProduct.categoryId}&name=${this.addProduct.name}&subtitle=${this.addProduct.subtitle}&subImages=${this.addProduct.subImages}&detail=${this.addProduct.detail}&price=${this.addProduct.price}&stock=${this.addProduct.stock}&status=1`
      );
      if (res.status !== 0) return this.$message.error("新增产品失败");
      this.$message.success("新增产品成功");
      this.$router.push("/permission");
    },
    
  },
  beforeCreate() {},
  created() {},
  beforeMount() {},
  mounted() {
    this.getSelectOne();
  },
};
</script> 
<style scoped>
</style>